package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.FileUtils;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/5/20.
 * Describe:
 */
public class UploadFileActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn_select)
    Button btnSelect;
    @BindView(R.id.tv_show)
    TextView tvShow;

    private final int CHOOSE_FILE_CODE = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_upload_file;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_upload_file));
        tvShow.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                tvShow.setText("");
                return true;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch(requestCode){
                case CHOOSE_FILE_CODE:
                    Uri uri = data.getData();
                    String filePath = FileUtils.getPathFromUri(this,uri);
                    System.out.println("输出filePath：" + filePath);
                    if(StringUtil.checkIsNotNull(filePath)){
                        tvShow.setText(filePath);
                        File file = new File(filePath);
                        if(file.exists()){
                            String upLoadFilePath = file.toString();
                            System.out.println("输出upLoadFilePath：" + upLoadFilePath);
                            String upLoadFileName = file.getName();
                            System.out.println("输出upLoadFileName：" + upLoadFileName);
                        }
                    }
                    break;

                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.btn_select, R.id.btn_upload})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_select:
                selectFileFromMobile();
                break;
            case R.id.btn_upload:
                MyToast.toastNormal(this,"暂时没有上传文件的接口可供测试");
                break;
        }
    }

    //从手机文件管理器中选择文件
    private void selectFileFromMobile() {
        //现获取权限
        requestPermission(1, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
                try {
                    startActivityForResult(Intent.createChooser(intent, "Choose File"), CHOOSE_FILE_CODE);
                } catch (ActivityNotFoundException e) {
                    MyToast.toastError(UploadFileActivity.this, "亲，木有文件管理器啊-_-!!",false);
                }
            }
        }, new Runnable() {
            @Override
            public void run() {
                MyToast.toastError(UploadFileActivity.this, "亲， 没有权限",false);
            }
        });
    }

}
